package org.vordel.incubator.examples.management_api;

import java.util.Collection;
import java.util.List;

import com.vordel.es.ESPK;
import com.vordel.es.Entity;
import com.vordel.es.EntityStore;
import com.vordel.es.EntityType;
import com.vordel.es.Field;
import com.vordel.es.FieldType;
import com.vordel.es.Value;
import com.vordel.es.xes.PortableESPK;
import com.vordel.es.xes.PortableESPKFactory;
import com.vordel.trace.Trace;

public abstract class EntityWrapper {

	
	

	EntityWrapper(Entity entity, EntityWrapperManager manager) {
		super();
		this.entity = entity;
		this.manager = manager;
	}
	
	
	protected Entity entity;
	protected EntityWrapperManager manager;
	
	public String getFieldValue(String name) {
		return this.entity.getField(name).getValues()[0].getData();
	}
	
	public void setFieldValue(String fieldName, String value) throws Exception {
		
		FieldType fieldType = this.getEntity().getType().getFieldType(fieldName);
		Field field = new Field(fieldType,fieldName);
		
		Value v = new Value(value);
		
		field.addValue(v);
		
		this.entity.setField(field);
		
		this.manager.getSession().getEntityStore().updateEntity(this.entity);
		
		
	}
	
	
	
	public void setReferenceField(String fieldName, EntityWrapper wrapper) throws Exception {
		
		FieldType fieldType = this.getEntity().getType().getFieldType(fieldName);
		
		boolean isSoftRef = false;
		if (fieldType.isSoftRefType()) {
			System.out.println("Field "+fieldName+" is a softref");
			isSoftRef = true;
		} else {
			System.out.println("Field "+fieldName+" is not a soft ref");
			
		}
		
		
		if (!isSoftRef) {
			//val = new Value(wrapper.getEntity().getPK());
			this.entity.setReferenceField(fieldName, wrapper.getEntity().getPK());
		} else {
			PortableESPKFactory pef = PortableESPKFactory.newInstance();
			PortableESPK pESPK = pef.createPortableESPK(this.manager.session.entityStore, wrapper.getEntity().getPK());
			this.entity.setReferenceField(fieldName, pESPK);
		}
		
		
		this.manager.session.getEntityStore().updateEntity(this.entity);
	}
	
	public void setReferenceField(String fieldName, EntityWrapper [] wrappers) throws Exception {
		
		
		FieldType fieldType = this.getEntity().getType().getFieldType(fieldName);
		
		boolean isSoftRef = false;
		if (fieldType.isSoftRefType()) {
			System.out.println("Field "+fieldName+" is a softref");
			isSoftRef = true;
		} else {
			System.out.println("Field "+fieldName+" is not a soft ref");
			
		}
		
		Value [] vals = new Value[wrappers.length];
		
		for (int i=0; i<wrappers.length; i++) {
			
			System.out.println("SetReferenceField "+i+"  wrapper="+wrappers[i]+" pk="+wrappers[i].getPK()+" entity="+wrappers[i].getEntity().getPK());
			
			if (!isSoftRef) {
				vals[i] = new Value(wrappers[i].getEntity().getPK());
			} else {
				PortableESPKFactory pef = PortableESPKFactory.newInstance();
				PortableESPK pESPK = pef.createPortableESPK(this.manager.session.entityStore, wrappers[i].getEntity().getPK());
				vals[i] = new Value(pESPK);
			}
		}
		
		
		this.entity.setField(fieldName,vals);
		this.manager.session.getEntityStore().updateEntity(this.entity);
	}
	
	public String getName() {
		return this.getFieldValue("name");
	}

	Entity getEntity() {
		return entity;
	}
	
	public void addChild(EntityWrapper child) throws Exception {
		
		EntityStore es = this.manager.session.getEntityStore();
		
		ESPK newESPK = es.addEntity(this.entity.getPK(), child.entity);
			
		child.entity = es.getEntity(newESPK);
		
		this.entity = es.getEntity(this.entity.getPK());
		
		Collection<ESPK> children = es.listChildren(this.entity.getPK(), child.entity.getType());
		
		Trace.info(this.getName()+" now has "+children.size()+" children");
		
	}
	
	public String getPK() {
		return this.entity.getPK().toString();
	}
	
}
